<?php

namespace App\Http\Controllers;

use App\Goods;
use Illuminate\Http\Request;
use Iwanli\Wxxcx\Wxxcx;

class ShopController extends Controller
{
    protected $wxxcx;

    function __construct(Wxxcx $wxxcx)
    {
        $this->wxxcx = $wxxcx;
    }


    public function getAll() {
        $goods=Goods::all();
        return response()->json($goods);
    }
    public function userInfo(Request $rq) {
        //使用 ajax 请求将获取的加密数据和参数发送到这里

        //code 在小程序端使用 wx.login 获取
        $code = $rq->input('code');
        //encryptedData 和 iv 在小程序端使用 wx.getUserInfo 获取
        $encryptedData = $rq->input('encryptedData');
        $iv = $rq->input('iv');

        //小程序类实例化
        $userInfo = $this->wxxcx->getLoginInfo($code);

        //获取解密后的用户信息
        return $this->wxxcx->getUserInfo($encryptedData, $iv);
    }

    public function getColors($id) {
        $colors=Goods::find($id)->colors;
        return response()->json($colors);
    }
    public function getSizes($id) {
        $sizes=Goods::find($id)->sizes;
        return response()->json($sizes);
    }
    public function getPics($id) {
        $pics=Pic::find($id)->pics;
        return response()->json($pics);
    }
    public function getCurrent($page,$company) {
        $pages=$page*10-10;
        $goods=Goods::where('company',$company)->orderBy('id','desc')->skip($pages)->take(10)->get();
        return response()->json($goods);
    }
    public function one($id) {
        $goods=Goods::find($id);
        return response()->json($goods);
    }

    public function getCount($company) {
        $goods=Goods::where('company',$company)->count();
        return response()->json($goods);
    }
    public function add( Request $request) {


    }
    public function del ( Request $request) {


    }
    public function inputPost ( Request $request) {

        if ($request->hasFile('inputGoods')) {
            $file = $request->file('inputGoods');
            $entension = $file->getClientOriginalExtension(); //上传文件的后缀.
            $name = date('ymdhis')."." . $entension;
            $request->file('inputGoods')->move('public', $name);


        }
        Excel::load('public/'.$name, function($reader) {

            $data= $reader->all();
            dd($data);
            foreach ($data as $d){
                $input['name']=$d[1];
                $input['goodsCode']=$d[0];
                $input['goodsType']=$d[2];
                $input['goodsUnit']=$d[3];
                $input['goodsNotes']=$d[4];
                if($d[5] == 1){
                    Goodsfirst::create($input);
                }elseif($d[5] == 2){
                    Goodssecond::create($input);
                }elseif($d[5] == 3){
                    Goodsthird::create($input);
                }
            }

        });

    }
    public function update($id, Request $request) {


    }
    public function search($word) {

    }
    public function page($page,$pagesize) {

    }

}
